﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Common;
using Zocono.WCS.Admin.Models.Equipment;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Application.AppEquipmentLayoutManage;
using Zocono.WCS.Application.AppEquipmentManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WCS.Infrastructure.ApllicationDto.Enums;

namespace Zocono.WCS.Admin.Controllers
{
    [BasicAuth]
    public class EquipmentController : BaseController
    {
        private readonly EquipmentApp _equipmentApp;
        private readonly EquipmentLayoutApp _equipmentLayoutApp;

        public EquipmentController(EquipmentApp equipmentApp, EquipmentLayoutApp equipmentLayoutApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            this._equipmentApp = equipmentApp;
            this._equipmentLayoutApp = equipmentLayoutApp;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 设备类型列表
        /// </summary>
        /// <returns></returns>
        public ActionResult EquipmentAsync()
        {
            //await AddTracesInfo();
            return View();
        }

        /// <summary>
        /// 获取所有的设备类型
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetAllEquipmentTypeAsync()
        {

            var where = new QueryEquipmentTypeInfoDto();
            //where.PageIndex = (Start / Length) + 1;
            //where.PageSize = Length;
            where.TypeName = Request.Form["name"].ToString();
            var eqList = await _equipmentApp.GetEquipmentTypeAsync(where);
            var pageData = new TableData<EquipmentTypeInfoDto>(Draw, eqList.Count, eqList.Count, eqList);
            //await AddTracesInfo(5, 0, "获取设备类型列表");
            return Json(pageData);
        }

        /// <summary>
        /// 添加或者编辑设备类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> EquipmentAddOrEdit(long id)
        {
            var equipmentTypeDto = new EquipmentTypeInfoDto();
            if (id != 0)
                equipmentTypeDto = await _equipmentApp.GetEquipmentTypeByIDAsync(id);
            return View(equipmentTypeDto);
        }
        /// <summary>
        /// 提交修改设备类型
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> OperateEquipmentTypeAsync()
        {
            var equipmentTypeDto = new EquipmentTypeInfoDto();
            equipmentTypeDto.ID = Request.Form["id"].ToInt64();
            equipmentTypeDto.TypeCode = Request.Form["code"].ToString();
            equipmentTypeDto.TypeName = Request.Form["name"].ToString();
            equipmentTypeDto.IsDispatch = Request.Form["enable"].ToInt32() == 1 ? true : false;
            bool result;
            string msg;
            try
            {
                if(equipmentTypeDto.ID == 0)
                {
                    var raw = await _equipmentApp.AddEquipmentTypeAsync(equipmentTypeDto);
                    await AddTracesInfo(2, 0, "新增设备类型配置");
                    result = raw > 0;
                }else
                    result = await _equipmentApp.UpdateEquipmentTypeAsync(equipmentTypeDto);
                await AddTracesInfo(3, 0, "修改设备类型配置");
                msg = result ? "成功" : "失败";
            }
            catch(Exception e)
            {
                result = false;
                msg = e.Message;
            }
            return CreateJsonGetResult(result, msg);
        }

        /// <summary>
        /// 删除设备类型
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> DelEquipmentTypeAsync()
        {
            var id = Request.Form["id"].ToInt64();
            bool result;
            string msg;
            try
            {
                result = await _equipmentApp.DeleteEquipmentTypeAsync(id);
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            await AddTracesInfo(4, 0, "删除设备类型配置");
            return CreateJsonGetResult(result, msg);
        }

        /// <summary>
        /// 获取设备类型列表生成 selectitem项 设置默认选择  针对调度设备业务 TODO未使用 暂留
        /// </summary>
        /// <param name="id"></param>
        public async void SetEqTypeToViewData(long id)
        {

            var eqList = await _equipmentApp.GetEquipmentTypeAsync(new QueryEquipmentTypeInfoDto());
            //var list = eqList.Result;
            List<EquipmentTypePage> eqPage = new List<EquipmentTypePage>();
            foreach (var item in eqList)
            {
                eqPage.Add(new EquipmentTypePage { ID = item.ID.ToString(), TypeName = item.TypeName });
            }
            ViewData["eqTypeList"] = eqPage;
        }

        /// <summary>
        /// 获取设备类型列表生成 selectitem项 设置默认选择  针对设备布局业务 TODO未使用 暂留
        /// </summary>
        /// <param name="id"></param>
        public async void SetEqLayoutTypeToViewData(long id)
        {
            var eqList = await _equipmentApp.GetEquipmentTypeAsync(new QueryEquipmentTypeInfoDto());
            //var list = eqList.Result;
            List<EquipmentTypePage> eqPage = new List<EquipmentTypePage>();
            foreach (var item in eqList)
            {
                eqPage.Add(new EquipmentTypePage { ID = item.ID.ToString(), TypeName = item.TypeName });
            }
            ViewData["eqTypeList"] = eqPage;
            var eqls = await _equipmentLayoutApp.GetEquipmentLayoutTypeByIDAsync(id);
            var selectId = "0";
            if (eqls != null)
            {
                selectId = eqls.EquipmentTypeID.ToString();
            }
            var eqListSelect = new SelectList(eqList, "ID", "TypeName", selectId);
            ViewData["eqLayoutList"] = eqListSelect;
        }

        /// <summary>
        /// 调度设备列表
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> DispatchEquipmentAsync()
        {
            //SetEqTypeToViewData(0);
            var eqList = await _equipmentApp.GetEquipmentTypeAsync(new QueryEquipmentTypeInfoDto());
            //var list = eqList.Result;
            List<EquipmentTypePage> eqPage = new List<EquipmentTypePage>();
            foreach (var item in eqList)
            {
                eqPage.Add(new EquipmentTypePage { ID = item.ID.ToString(), TypeName = item.TypeName });
            }
            //await AddTracesInfo();
            return View(eqPage);
        }

        /// <summary>
        /// 获取所有调度设备
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetAllDispatchEquipmentAsync()
        {
            Console.WriteLine(Request);
            var where = new QueryDispatchEquipmentInfoDto();
            where.PageIndex =  (Start / Length) + 1;
            where.PageSize = Length;
            where.EquipmentName = Request.Form["name"].ToString();
            where.EquipmentTypeID = Request.Form["type"].ToInt64();
            var disEqList = await _equipmentApp.GetDispatchEquipmentAsync(where);
            var pageData = new TableData<DispatchEquipmentInfoDto>(Draw, disEqList.Total, disEqList.Total, disEqList.Result);
            //var disEqList = await _equipmentApp.GetALlDispatchEquipmentAsync();
            //var pageData = new TableData<DispatchEquipmentInfoDto>(Draw, disEqList.Count, disEqList.Count, disEqList);
            //await AddTracesInfo(5, 0, "获取调度设备列表");
            return Json(pageData);
        }

        /// <summary>
        /// 编辑调度设备
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> DispatchEquipmentAddOrEdit(long id)
        {
           var pageData = new EquipmentPageData();
            var eqList = await _equipmentApp.GetEquipmentTypeAsync(new QueryEquipmentTypeInfoDto());
            //var list = eqList.Result;
            pageData.EquipmentTypes = new List<EquipmentTypePage>();
            foreach (var item in eqList)
            {
                pageData.EquipmentTypes.Add(new EquipmentTypePage { ID = item.ID.ToString(), TypeName = item.TypeName });
            }
            if (id > 0)
            {
                var dispatchEquipmentDto = await _equipmentApp.GetDispatchEquipmentByIDAsync(id);
                pageData.ID = dispatchEquipmentDto.ID;
                pageData.AreaCode = dispatchEquipmentDto.AreaCode;
                pageData.EquipmentCode = dispatchEquipmentDto.EquipmentCode;
                pageData.EquipmentName = dispatchEquipmentDto.EquipmentName;
                pageData.EquipmentTypeID = dispatchEquipmentDto.EquipmentTypeID;
                pageData.EquipmentTypeName = dispatchEquipmentDto.EquipmentTypeName;
                pageData.StationCount = dispatchEquipmentDto.StationCount;
            }

            return View(pageData);
        }

        /// <summary>
        /// 编辑或者添加调度设备执行
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> OperateDispatchEquipmentAsync()
        {
            var dispatchEquipmentDto = new DispatchEquipmentInfoDto();
            dispatchEquipmentDto.ID = Request.Form["id"].ToInt64();
            dispatchEquipmentDto.EquipmentCode = Request.Form["code"].ToString();
            dispatchEquipmentDto.EquipmentName = Request.Form["name"].ToString();
            dispatchEquipmentDto.AreaCode = Request.Form["area"].ToString();
            dispatchEquipmentDto.StationCount = Request.Form["station"].ToInt32();
            dispatchEquipmentDto.EquipmentTypeID = Request.Form["eqTypeId"].ToInt64();

            bool result;
            string msg;
            try
            {
                if (dispatchEquipmentDto.ID == 0)
                {
                    var res = await _equipmentApp.AddDispatchEquipmentAsync(dispatchEquipmentDto);
                    await AddTracesInfo(2, 0, "新增调度设备");
                    result = res > 0;
                }
                else
                {
                    result = await _equipmentApp.UpdateDispatchEquipmentAsync(dispatchEquipmentDto);
                    await AddTracesInfo(2, 0, "修改调度设备");
                }
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }

            return CreateJsonGetResult(result, msg);
        }

        /// <summary>
        /// 删除调度设备
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> DelDispatchEquipmentAsync()
        {
            var id = Request.Form["id"].ToInt64();
            bool result;
            string msg;
            try
            {
                result = await _equipmentApp.DeleteDispatchEquipmentAsync(id);
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            await AddTracesInfo(4, 0, "删除调度设备");
            return CreateJsonGetResult(result, msg);
        }

        /// <summary>
        /// 设备布局类型列表
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> EquipmentLayoutTypeAsync()
        {
            //SetEqLayoutTypeToViewData(0);
            var eqList = await _equipmentApp.GetEquipmentTypeAsync(new QueryEquipmentTypeInfoDto());
            //var list = eqList.Result;
            List<EquipmentTypePage> eqPage = new List<EquipmentTypePage>();
            foreach (var item in eqList)
            {
                eqPage.Add(new EquipmentTypePage { ID = item.ID.ToString(), TypeName = item.TypeName });
            }
            //await AddTracesInfo();
            return View(eqPage);
        }

        /// <summary>
        /// 获取设备布局类型列表
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetAllEquipmentLayoutType()
        {
            var where = new QueryEquipmentLayoutTypeInfoDto();
            where.PageIndex = (Start / Length) + 1;
            where.PageSize = Length;
            where.TypeName = Request.Form["name"].ToString();
            where.EquipmentTypeID = Request.Form["type"].ToInt64();
            var eqLayoutTypes = await _equipmentLayoutApp.GetEquipmentLayoutTypeAsync(where);
            var pageData = new TableData<EquipmentLayoutTypeInfoDto>(Draw, eqLayoutTypes.Total, eqLayoutTypes.Total, eqLayoutTypes.Result);

            await AddTracesInfo(5, 0, "获取设备布局类型");
            return Json(pageData);
        }

        /// <summary>
        /// 编辑或者添加设备布局类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> EquipmentLayoutTypeAddOrEditAsync(long id)
        {
            var pageData = new EquipmentLayoutTypePageData();
            var eqList = await _equipmentApp.GetEquipmentTypeAsync(new QueryEquipmentTypeInfoDto());
            //var list = eqList.Result;
            pageData.EquipmentTypes = new List<EquipmentTypePage>();
            foreach (var item in eqList)
            {
                pageData.EquipmentTypes.Add(new EquipmentTypePage { ID = item.ID.ToString(), TypeName = item.TypeName });
            }
            if (id > 0)
            {
                var equipmentLayoutType = await _equipmentLayoutApp.GetEquipmentLayoutTypeByIDAsync(id);
                pageData.ID = equipmentLayoutType.ID;
                pageData.TypeName = equipmentLayoutType.TypeName;
                pageData.EquipmentTypeID = equipmentLayoutType.EquipmentTypeID;
                pageData.EquipmentTypeName = equipmentLayoutType.EquipmentTypeName;
                pageData.TypeCode = equipmentLayoutType.TypeCode;
            }
            return View(pageData);
        }



        ///// <summary>
        ///// 设备类型列表
        ///// </summary>
        ///// <returns></returns>
        //public async Task<ActionResult> EquipmentTypeAsync()
        //{
        //    //SetEqLayoutTypeToViewData(0);
        //    var eqList = await _equipmentApp.GetEquipmentTypeAsync(new QueryEquipmentTypeInfoDto());
        //    //var list = eqList.Result;
        //    List<EquipmentTypePage> eqPage = new List<EquipmentTypePage>();
        //    foreach (var item in eqList)
        //    {
        //        eqPage.Add(new EquipmentTypePage { ID = item.ID.ToString(), TypeName = item.TypeName });
        //    }
        //    await AddTracesInfo();
        //    return View(eqPage);
        //}

        ///// <summary>
        ///// 获取设备类型列表
        ///// </summary>
        ///// <returns></returns>
        //public async Task<JsonResult> GetAllEquipmentType()
        //{
        //    var where = new QueryEquipmentLayoutTypeInfoDto();
        //    where.PageIndex = (Start / Length) + 1;
        //    where.PageSize = Length;
        //    where.TypeName = Request.Form["name"].ToString();
        //    where.EquipmentTypeID = Request.Form["type"].ToInt64();
        //    var eqLayoutTypes = await _equipmentLayoutApp.GetEquipmentTypeAsync(where);
        //    var pageData = new TableData<EquipmentTypeInfoDto>(Draw, eqLayoutTypes.Total, eqLayoutTypes.Total, eqLayoutTypes.Result);

        //    await AddTracesInfo(5, 0, "获取设备布局类型");
        //    return Json(pageData);
        //}

        ///// <summary>
        ///// 编辑或者添加设备类型
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public async Task<ActionResult> EquipmentTypeAddOrEditAsync(long id)
        //{
        //    var pageData = new EquipmentLayoutTypePageData();
        //    var eqList = await _equipmentApp.GetEquipmentTypeAsync(new QueryEquipmentTypeInfoDto());
        //    //var list = eqList.Result;
        //    pageData.EquipmentTypes = new List<EquipmentTypePage>();
        //    foreach (var item in eqList)
        //    {
        //        pageData.EquipmentTypes.Add(new EquipmentTypePage { ID = item.ID.ToString(), TypeName = item.TypeName });
        //    }
        //    if (id > 0)
        //    {
        //        var equipmentLayoutType = await _equipmentLayoutApp.GetEquipmentLayoutTypeByIDAsync(id);
        //        pageData.ID = equipmentLayoutType.ID;
        //        pageData.TypeName = equipmentLayoutType.TypeName;
        //        pageData.EquipmentTypeID = equipmentLayoutType.EquipmentTypeID;
        //        pageData.EquipmentTypeName = equipmentLayoutType.EquipmentTypeName;
        //        pageData.TypeCode = equipmentLayoutType.TypeCode;
        //    }
        //    return View(pageData);
        //}








        /// <summary>
        /// 执行编辑添加设备布局类型操作
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> OperateEquipmentLayoutTypeAsync()
        {
            var equipmentLayoutTypeDto = new EquipmentLayoutTypeInfoDto();
            equipmentLayoutTypeDto.ID = Request.Form["id"].ToInt64();
            equipmentLayoutTypeDto.TypeCode = Request.Form["code"].ToString();
            equipmentLayoutTypeDto.TypeName = Request.Form["name"].ToString();
            equipmentLayoutTypeDto.EquipmentTypeID = Request.Form["eqTypeId"].ToInt64();

            bool result;
            string msg;
            try
            {
                if (equipmentLayoutTypeDto.ID == 0)
                {
                    var res = await _equipmentLayoutApp.AddEquipmentLayoutTypeAsync(equipmentLayoutTypeDto);
                    await AddTracesInfo(2, 0, "新增设备布局类型");
                    result = res > 0;
                }
                else
                {
                    result = await _equipmentLayoutApp.UpdateEquipmentLayoutTypeAsync(equipmentLayoutTypeDto);
                    await AddTracesInfo(3, 0, "修改设备布局类型");
                }
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }

            return CreateJsonGetResult(result, msg);
        }

        /// <summary>
        /// 删除设备布局类型
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> DelEquipmentLayoutTypeAsync()
        {
            var id = Request.Form["id"].ToInt64();
            bool result;
            string msg;
            try
            {
                result = await _equipmentLayoutApp.DeleteEquipmentLayoutTypeAsync(id);
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            await AddTracesInfo(4, 0, "删除设备布局类型");
            return CreateJsonGetResult(result, msg);
        }

        /// <summary>
        /// 设备布局列表
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> EquipmentLayoutAsync()
        {
            //SetEqLayoutTypeToViewData(0);
            Dictionary<string, object> dropDic = EnumHelper.EnumListDic<EnumEquipmentDirection>("", "");
            List<EquipmentDirectionPage> listPage = new List<EquipmentDirectionPage>();
            if (dropDic != null)
            {
                foreach (var item in dropDic)
                {
                    listPage.Add(new EquipmentDirectionPage { ID = item.Value.ToInt32(), Desception = item.Key.ToString() });
                }
            }
            ViewData["directions"] = listPage;
            //await AddTracesInfo();
            return View();
        }

        /// <summary>
        /// 获取设备布局列表数据
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetEquipmentLayoutAsync()
        {
            var where = new QueryEquipmentLayoutInfoDto();
            where.PageIndex = (Start / Length) + 1;
            where.PageSize = Length;
            where.AreaCode = Request.Form["area"].ToString();
            where.Direction = Request.Form["dir"].ToInt32();
            var eqLayoutInfos = await _equipmentLayoutApp.GetEquipmentLayoutAsync(where);
            var pageData = new TableData<EquipmentLayoutInfoDto>(Draw, eqLayoutInfos.Total, eqLayoutInfos.Total, eqLayoutInfos.Result);

            //await AddTracesInfo(5, 0, "设备布局列表");

            return Json(pageData);
        }


        /// <summary>
        /// 添加或者编辑设备布局界面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> EquipmentLayoutAddOrEditAsync(long id)
        {
            var pageData = new EquipmentLayoutPageData();
            //调度设备列表
            var disEqList = await _equipmentApp.GetALlDispatchEquipmentAsync();
            if (disEqList != null && disEqList.Count > 0)
            {
                pageData.DispatchEquipments = new List<EquipmentPage>();
                foreach (var item in disEqList)
                {
                    pageData.DispatchEquipments.Add(new EquipmentPage { EquipmentID = item.ID.ToString(), EquipmentName = item.EquipmentName });
                }
            }
            //设备布局类别列表
            var eqLayoutTypeList = await _equipmentLayoutApp.GetEquipmentLayoutTypeAsync(new QueryEquipmentLayoutTypeInfoDto(){ PageIndex = 1, PageSize = int.MaxValue });
            if (eqLayoutTypeList != null && eqLayoutTypeList.Result != null && eqLayoutTypeList.Result.Count > 0)
            {
                pageData.EquipmentLayoutTypes = new List<EquipmentLayoutTypePage>();
                foreach (var item in eqLayoutTypeList.Result)
                {
                    pageData.EquipmentLayoutTypes.Add(new EquipmentLayoutTypePage { EquipmentLayoutTypeID = item.ID.ToString(), EquipmentLayoutTypeName = item.TypeName });
                }
            }

            Dictionary<string, object> dropDic = EnumHelper.EnumListDic<EnumEquipmentDirection>("", "");
            if (dropDic != null)
            {
                pageData.EquipmentDirections = new List<EquipmentDirectionPage>();
                foreach (var item in dropDic)
                {
                    pageData.EquipmentDirections.Add(new EquipmentDirectionPage { ID = item.Value.ToInt32(), Desception = item.Key.ToString() });
                }
            }

            if (id > 0)
            {
                var equipmentLayoutDto = await _equipmentLayoutApp.GetEquipmentLayoutByIDAsync(id);
                pageData.ID = equipmentLayoutDto.ID.ToString();
                pageData.AreaCode = equipmentLayoutDto.AreaCode;
                pageData.ColumnCount = equipmentLayoutDto.ColumnCount;
                pageData.DefaultColor = equipmentLayoutDto.DefaultColor;
                pageData.Direction = equipmentLayoutDto.Direction;
                pageData.DirectionDescirption = equipmentLayoutDto.DirectionDescirption;
                pageData.EquipmentID = equipmentLayoutDto.EquipmentID;
                pageData.EquipmentLayoutCode = equipmentLayoutDto.EquipmentLayoutCode;
                pageData.EquipmentLayoutTypeID = equipmentLayoutDto.EquipmentLayoutTypeID;
                pageData.EquipmentLayoutTypeName = equipmentLayoutDto.EquipmentLayoutTypeName;
                pageData.EquipmentName = equipmentLayoutDto.EquipmentName;
                pageData.PointX = equipmentLayoutDto.PointX;
                pageData.PointY = equipmentLayoutDto.PointY;
                pageData.SizeHeigth = equipmentLayoutDto.SizeHeigth;
                pageData.SizeWidth = equipmentLayoutDto.SizeWidth;
            }
            return View(pageData);
        }

        /// <summary>
        /// 添加或者编辑设备布局
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> OperateEquipmentLayoutAsync()
        {
            var equipmentLayoutDto = new EquipmentLayoutInfoDto();

            equipmentLayoutDto.ID = Request.Form["id"].ToInt64();
            equipmentLayoutDto.AreaCode = Request.Form["area"].ToString();
            equipmentLayoutDto.ColumnCount = Request.Form["column"].ToInt32();
            equipmentLayoutDto.DefaultColor = Request.Form["color"].ToString();
            equipmentLayoutDto.Direction = Request.Form["dir-Id"].ToInt32();
            equipmentLayoutDto.EquipmentID = Request.Form["dispatch-eq-Id"].ToInt64();
            equipmentLayoutDto.EquipmentLayoutCode = Request.Form["layoutCode"].ToString();
            equipmentLayoutDto.EquipmentLayoutTypeID = Request.Form["eq-layout-type-Id"].ToInt64();
            equipmentLayoutDto.PointX = Request.Form["pos-x"].ToInt32();
            equipmentLayoutDto.PointY = Request.Form["pos-y"].ToInt32();
            equipmentLayoutDto.SizeHeigth = Request.Form["max-height"].ToInt32();
            equipmentLayoutDto.SizeWidth = Request.Form["max-width"].ToInt32();

            bool result;
            string msg;
            try
            {
                if (equipmentLayoutDto.ID == 0)
                {
                    var res = await _equipmentLayoutApp.AddEquipmentLayoutAsync(equipmentLayoutDto);
                    await AddTracesInfo(2, 0, "新增设备布局类型");
                    result = res > 0;
                }
                else
                {
                    result = await _equipmentLayoutApp.UpdateEquipmentLayoutAsync(equipmentLayoutDto);
                    await AddTracesInfo(3, 0, "修改设备布局类型");
                }
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }

            return CreateJsonGetResult(result, msg);
        }

        /// <summary>
        /// 删除设备布局
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> DelEquipmentLayoutAsync()
        {
            var id = Request.Form["id"].ToInt64();
            bool result;
            string msg;
            try
            {
                result = await _equipmentLayoutApp.DeleteEquipmentLayoutAsync(id);
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            await AddTracesInfo(4, 0, "删除设备布局");
            return CreateJsonGetResult(result, msg);
        }
    }

}
